/**
*  \file canmsg_cfg.h
*
*  \brief
*
*  \author Michael J. Hohmann  <michael.hohmann@linde-mh.de>
*
*
*/

#ifndef CANMSG_CFG_H_INCLUDED
#define CANMSG_CFG_H_INCLUDED

/*================================================[ public includes  ]================================================*/
#include "../cfg/prj.h"

/*================================================[ public defines   ]================================================*/

/*================================================[ public datatypes ]================================================*/


typedef U32 canmsg_id_t;


struct canmsg
{
   U8 port;
   U8 mailbox_no;
   U8 dlc;
   U8 extended;

   union
   {
      struct
      {
         U32 node_id : 7;
         U32 command : 4;
      } can_open;
      U32 full;
   } id;
   union
   {
      U32 u32data[2];
      U16 u16data[4];
      U8  u8data[8];
      struct
      {
         U8 command;
         U8 id[3];
         U32 value;
      } sdo;
   }  store;
} ;

typedef struct canmsg can_mailbox;
typedef struct canmsg canmsg_t;


/*================================================[ public variables ]================================================*/

/*================================================[ inline functions ]================================================*/

STATIC_INLINE U8 * canmsg_data_ptr( struct canmsg *msg )
{
   return msg->store.u8data;
}

STATIC_INLINE U8 canmsg_get_dlc( struct canmsg *msg )
{
   return msg->dlc;
}

STATIC_INLINE U8 canmsg_get_port( struct canmsg *msg )
{
   return msg->port;
}


STATIC_INLINE canmsg_id_t canmsg_get_id( struct canmsg *msg )
{
   return msg->id.full;
}

STATIC_INLINE void canmsg_set_dlc( struct canmsg *msg, U8 value )
{
   msg->dlc = value;
}

STATIC_INLINE void canmsg_set_id( struct canmsg *msg, canmsg_id_t id )
{
   msg->id.full = id;
}

STATIC_INLINE U8* canmsg_set( struct canmsg *msg, canmsg_id_t id, U8 dlc )
{
   pal_memzero(msg,sizeof *msg);
   canmsg_set_id(msg,id);
   canmsg_set_dlc(msg,dlc);
   return canmsg_data_ptr(msg);
}


#define CANMSG_SIZEOF_DATA U32_CONST(8)






/*================================================[ public functions ]================================================*/

/*====================================================================================================================*/

#endif

/***********************************************************************************************************************
*                             Copyright 2018 Linde Material Handling. All rights reserved.                             *
***********************************************************************************************************************/
